home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / initramfs-tools / hook-functions next >
Encoding:
Text File  |  2009-04-14  |  11.0 KB  |  444 lines

  1. # -*- shell-script -*-
  2.  
  3. catenate_cpiogz() {
  4.     # Sanity check
  5.     if [ ! -e "${1}" ]; then
  6.         echo "W:catenate_cpiogz: arg1='${1}' does not exist." >&2
  7.         return
  8.     fi
  9.  
  10.     cat "${1}" >>"${__TMPCPIOGZ}"
  11. }
  12.  
  13. force_load()
  14. {
  15.         manual_add_modules ${@}
  16.         echo "${@}" >>"${DESTDIR}/conf/modules"
  17. }
  18.  
  19. # Takes a file containing a list of modules to be added as an
  20. # argument, figures out dependancies, and adds them.
  21. #
  22. # Input file syntax:
  23. #
  24. #   # comment
  25. #   modprobe_module_name [args ...]
  26. #   [...]
  27. #
  28. add_modules_from_file()
  29. {
  30.     # Sanity check
  31.     if [ ! -e "${1}" ]; then
  32.         echo "W:add_modules_from_file: arg1='${1}' does not exist." >&2
  33.         return
  34.     fi
  35.  
  36.     sed -e '/^#/d' ${1} | while read module rest; do
  37.         force_load "${module}" "${rest}"
  38.     done
  39. }
  40.  
  41. # Add dependent modules + eventual firmware
  42. manual_add_modules()
  43. {
  44.     local mam_x firmwares firmware
  45.  
  46.     for mam_x in $(modprobe --set-version="${version}" --ignore-install \
  47.     --show-depends "${1}" 2>/dev/null | awk '/^insmod/ { print $2 }'); do
  48.         # Prune duplicates
  49.         if [ -e "${DESTDIR}/${mam_x}" ]; then
  50.             continue
  51.         fi
  52.  
  53.         mkdir -p "${DESTDIR}/$(dirname "${mam_x}")"
  54.         ln -s "${mam_x}" "${DESTDIR}/$(dirname "${mam_x}")"
  55.         if [ "${verbose}" = "y" ]; then
  56.             echo "Adding module ${mam_x}"
  57.         fi
  58.  
  59.         # Add firmware files if necessary
  60.         firmwares=$(modinfo -F firmware "${mam_x}")
  61.         if [ -z "${firmwares}" ]; then
  62.             continue
  63.         fi
  64.         for firmware in $firmwares; do
  65.             if [ -e "${DESTDIR}/lib/firmware/${version}/${firmware}" ]; then
  66.                 continue
  67.             fi
  68.  
  69.             # Only print warning for missing fw of loaded module
  70.             # or forced loaded module
  71.             if [ ! -e "/lib/firmware/${version}/${firmware}" ]; then
  72.                 if grep -q "^$(basename "${mam_x}" .ko)" \
  73.                 /proc/modules \
  74.                 || grep -q "^$(basename "${mam_x}" .ko)" \
  75.                 "${CONFDIR}/modules"; then
  76.                     echo "W: Possible missing firmware /lib/firmware/${version}/${firmware} for module $(basename ${mam_x} .ko)" >&2
  77.                 fi
  78.                 continue
  79.             fi
  80.  
  81.             copy_exec "/lib/firmware/${version}/${firmware}"
  82.             if [ "${verbose}" = "y" ]; then
  83.                 echo "Adding firmware ${firmware}"
  84.             fi
  85.         done
  86.     done
  87. }
  88.  
  89. # $1 is the source path (e.g. /usr/bin/time)
  90. # $2 is the relative destination (e.g. /usr or /usr/time)
  91. #
  92. # The destination is interpreted in the same way "cp" would, meaning
  93. # (assuming /bin is a directory):
  94. #
  95. #   "copy_exec /usr/bin/time /bin"        -> /bin/time
  96. #   "copy_exec /usr/bin/time /bin/mytime" -> /bin/mytime
  97. # If $2 is left out, the same destination path as for the source arg will
  98. # be used and directories will be created as needed, so:
  99. #
  100. #   "copy_exec /usr/bin/time"             -> /usr/bin/time
  101. #
  102. copy_exec() {
  103.     local source target destination final_destination x nonoptlib
  104.     local libname dirname
  105.  
  106.     source="${1}"
  107.     if [ -n "${2}" ]; then
  108.         target="${2}"
  109.     else
  110.         if [ ! -e "${DESTDIR}/$(dirname "${1}")" ]; then
  111.             mkdir -p "${DESTDIR}/$(dirname "${1}")"
  112.         fi
  113.         target="${1}"
  114.     fi
  115.  
  116.     if [ -d "${DESTDIR}/${target}" ]; then
  117.         destination="${target}/$(basename "${source}")"
  118.     else
  119.         destination="${target}"
  120.     fi
  121.     final_destination="${DESTDIR}/${destination}"
  122.  
  123.     if [ -L "$final_destination" ]; then
  124.         if [ $(readlink "${final_destination}") != "${source}" ]; then
  125.             echo "W:copy_exec: Not copying ${source} to \$DESTDIR${destination}, which is already a copy of $(readlink ${final_destination})" >&2
  126.             return
  127.         fi
  128.     else
  129.         ln -s ${source} ${DESTDIR}/${destination}
  130.         if [ "${verbose}" = "y" ]; then
  131.             echo "Adding binary ${source}"
  132.         fi
  133.     fi
  134.  
  135.     # Copy the dependant libraries
  136.     for x in $(ldd ${source} 2>/dev/null | sed -e '
  137.         /\//!d;
  138.         /linux-gate/d;
  139.         /=>/ {s/.*=>[[:blank:]]*\([^[:blank:]]*\).*/\1/};
  140.         s/[[:blank:]]*\([^[:blank:]]*\) (.*)/\1/' 2>/dev/null); do
  141.  
  142.         # Try to use non-optimised libraries where possible.
  143.         # We assume that all HWCAP libraries will be in tls,
  144.         # sse2, vfp or neon
  145.         nonoptlib=$(echo "${x}" | sed -e 's#/lib/\(tls\|i686\|sse2\|neon\|vfp\).*/\(lib.*\)#/lib/\2#')
  146.  
  147.         if [ -e "${nonoptlib}" ]; then
  148.             x="${nonoptlib}"
  149.         fi
  150.  
  151.         libname=$(basename "${x}")
  152.         dirname=$(dirname "${x}")
  153.  
  154.         mkdir -p "${DESTDIR}/${dirname}"
  155.         if [ ! -e "${DESTDIR}/${dirname}/${libname}" ]; then
  156.             ln -s "${x}" "${DESTDIR}/${dirname}"
  157.             if [ "${verbose}" = "y" ]; then
  158.                 echo "Adding library ${x}"
  159.             fi
  160.         fi
  161.     done
  162. }
  163.  
  164. # Copy entire subtrees to the initramfs
  165. copy_modules_dir()
  166. {
  167.     local x_mod
  168.  
  169.     if ! [ -d "${MODULESDIR}/${1}" ]; then
  170.         return;
  171.     fi
  172.     if [ "${verbose}" = "y" ]; then
  173.         echo "Copying module directory ${1}"
  174.     fi
  175.     for x_mod in $(find "${MODULESDIR}/${1}" -name '*.ko' -print); do
  176.         manual_add_modules $(basename ${x_mod} .ko)
  177.     done
  178. }
  179.  
  180. # walk /sys for relevant modules
  181. sys_walk_mod_add()
  182. {
  183.     local driver_path module
  184.     device_path="$1"
  185.     
  186.     while [ "${device_path}" != "/sys" ]; do
  187.         driver_path="$(readlink -f ${device_path}/driver)"
  188.         if [ -e "$driver_path" ]; then
  189.             module="$(basename $(readlink -f $driver_path))"
  190.             if [ -n "${module}" ]; then
  191.                 force_load "${module}"
  192.             fi
  193.         fi
  194.         device_path="$(dirname ${device_path})"
  195.     done
  196. }
  197.  
  198. # walk /sys for relevant modalias
  199. sys_walk_modalias()
  200. {
  201.     local device_path modalias
  202.  
  203.     device_path="$(dirname "${1}")"
  204.     device_path="$(dirname "${device_path}")"
  205.     if [ -e "${device_path}/modalias" ]; then
  206.         modalias=$(cat "${device_path}/modalias")
  207.     fi
  208.  
  209.     if [ -n "${modalias}" ]; then
  210.         force_load "${modalias}"
  211.     fi
  212. }
  213.  
  214. # find and only copy root relevant modules
  215. dep_add_modules()
  216. {
  217.     local block minor root FSTYPE root_dev_path x
  218.  
  219.     # findout root block device + fstype
  220.     eval "$(mount | awk '/\/dev\// {if ($3 == "/") {print "root=" $1 "\nFSTYPE=" $5; exit}}')"
  221.     if [ "${root}" = "/dev/root" ] ; then
  222.         root="/dev/disk/by-uuid/"$(/lib/udev/vol_id --uuid ${root}) 2>/dev/null
  223.     fi
  224.     root="$(readlink -f ${root})"
  225.  
  226.     # find out real rootfs on auto type
  227.     if [ "${FSTYPE}" = "auto" ]; then
  228.         eval "$(/usr/lib/klibc/bin/fstype ${root})"
  229.     fi
  230.  
  231.     # check that fstype rootfs recognition
  232.     if [ "${FSTYPE}" = "unknown" ]; then
  233.         echo "mkinitramfs: unknown fstype on root ${root}"
  234.         echo "mkinitramfs: workaround is MODULES=most" 
  235.         echo "mkinitramfs: Error please report bug on initramfs-tools" 
  236.         exit 1
  237.     fi
  238.  
  239.     # Add rootfs
  240.     manual_add_modules "${FSTYPE}"
  241.  
  242.     # lvm luks root
  243.     if [ "${root#/dev/mapper/}" != "${root}" ]; then
  244.         minor=$((0x$(stat --format "%T" ${root}) % 256))
  245.         block=$(ls -1 /sys/block/dm-${minor}/slaves | head -n 1)
  246.         if [ "${block#dm-}" != "${block}" ]; then
  247.             block=$(ls -1 /sys/block/${block}/slaves | head -n 1)
  248.         fi
  249.         block=${block%[0-9]*}
  250.     # md root new naming scheme /dev/md/X
  251.     elif [ "${root#/dev/md/}" != "${root}" ]; then
  252.         root=${root#/dev/md/}
  253.         block=$(awk "/^md${root}/{print substr(\$5, 1, 3); exit}" \
  254.             /proc/mdstat)
  255.     # md root /dev/mdX
  256.     elif [ "${root#/dev/md}" != "${root}" ]; then
  257.         root=${root#/dev/}
  258.         block=$(awk "/^${root}/{print substr(\$5, 1, 3); exit}" \
  259.             /proc/mdstat)
  260.     # classical root device
  261.     else
  262.         block=${root#/dev/}
  263.         block=${block%[0-9]*}
  264.     fi
  265.  
  266.     # Error out if /sys lack block dev
  267.     if [ -z "${block}" ] || [ ! -e /sys/block/${block} ]; then
  268.         echo "mkinitramfs: missing ${block} root ${root} /sys entry"
  269.         echo "mkinitramfs: workaround is MODULES=most" 
  270.         echo "mkinitramfs: Error please report the bug" 
  271.         exit 1
  272.     fi
  273.  
  274.     # sys walk ATA
  275.     root_dev_path=$(readlink -f /sys/block/${block}/device)
  276.     sys_walk_mod_add ${root_dev_path}
  277.  
  278.     # catch old-style IDE
  279.     if [ -e /sys/bus/ide/devices/ ]; then
  280.         sys_walk_modalias ${root_dev_path}
  281.         manual_add_modules ide-disk
  282.         manual_add_modules ide-cd
  283.     fi
  284.  
  285.     if [ -e /sys/bus/scsi/devices/ ]; then
  286.         manual_add_modules sd_mod
  287.     fi
  288.  
  289.     if [ -e /sys/bus/i2o/devices/ ]; then
  290.         force_load i2o_block
  291.         force_load i2o_config
  292.     fi
  293.  
  294.     if [ -e /sys/bus/ps3_system_bus/ ]; then
  295.         for x in ps3disk ps3rom ps3-gelic ps3_sys_manager; do
  296.             manual_add_modules "${x}"
  297.         done
  298.     fi
  299.  
  300.     if [ -e /sys/bus/vio/ ]; then
  301.         for x in sunvnet sunvdc; do
  302.             manual_add_modules "${x}"
  303.         done
  304.     fi
  305. }
  306.  
  307.  
  308. # The modules "most" classes added per default to the initramfs
  309. auto_add_modules()
  310. {
  311.     case "$1" in
  312.     base)
  313.         for x in ehci-hcd ohci-hcd uhci-hcd usbhid hid_a4tech \
  314.         hid_apple hid_belkin hid_bright hid_cherry hid_chicony \
  315.         hid_cypress hid_dell hid_ezkey hid_gyration hid_logitech \
  316.         hid_microsoft hid_monterey hid_petalynx hid_pl     hid_samsung \
  317.         hid_sony hid_sunplus hid_tmff hid_zpff usb-storage ext2 \
  318.         ext3 ext4 isofs jfs nfs reiserfs udf xfs af_packet atkbd i8042 \
  319.         virtio_pci vfat nls_cp437 nls_iso8859-1; do
  320.             manual_add_modules "${x}"
  321.         done
  322.     ;;
  323.     net)
  324.         for x in 3c59x 8139cp 8139too 8390 atl1 atl1e b44 bmac \
  325.         bnx2 cxgb3 defxx dl2k e100 e1000 e1000e ehea epic100 \
  326.         ep93xx_eth eql fealnx famachi forcedeth gelic_net \
  327.         hp100 igb ipg mace mv643xx_eth myri10ge \
  328.         natsemi ne2k-pci netconsole niu ns83820 pcnet32 qla3xxx \
  329.         r8169 s2io sis900 skge sky2 slhc smc911x starfire \
  330.         sundance sungem sungem_phy sunhme sunvnet tg3 tlan de2104x \
  331.         de4x5 dmfe tulip winbond-840 xircom_cb xircom_tulip_cb \
  332.         typhon via-rhine via-velocity yellowfin; do
  333.             manual_add_modules "${x}"
  334.         done
  335.     ;;
  336.     ide)
  337.         copy_modules_dir kernel/drivers/ide
  338.     ;;
  339.     scsi)
  340.         copy_modules_dir kernel/drivers/scsi
  341.         for x in mptfc mptsas mptscsih mptspi; do
  342.             manual_add_modules "${x}"
  343.         done
  344.     ;;
  345.     ata)
  346.         copy_modules_dir kernel/drivers/ata
  347.     ;;
  348.     block)
  349.         copy_modules_dir kernel/drivers/block
  350.     ;;
  351.     # FIXME: can be removed after Lenny release
  352.     ieee1394)
  353.         for x in ohci1394 sbp2; do
  354.             manual_add_modules "${x}"
  355.         done
  356.     ;;
  357.     firewire)
  358.         for x in firewire-ohci  firewire-sbp2; do
  359.             manual_add_modules "${x}"
  360.         done
  361.     ;;
  362.     i2o)
  363.         for x in i2o_block; do
  364.             manual_add_modules "${x}"
  365.         done
  366.     ;;
  367.     dasd)
  368.         for x in dasd_eckd_mod dasd_fba_mod; do
  369.             manual_add_modules "${x}"
  370.         done
  371.     ;;
  372.     *)
  373.         auto_add_modules base
  374.         auto_add_modules net
  375.         auto_add_modules ide
  376.         auto_add_modules scsi
  377.         auto_add_modules block
  378.         auto_add_modules ata
  379.         auto_add_modules i2o
  380.         auto_add_modules dasd
  381.         auto_add_modules ieee1394
  382.         auto_add_modules firewire
  383.     ;;
  384.     esac
  385. }
  386.  
  387. usage()
  388. {
  389.     cat >&2 << EOF
  390.  
  391. Usage: ${0} [OPTION]... <-o outfile> [version]
  392.  
  393. Options:
  394.   -d confdir  Specify an alternative configuration directory.
  395.   -k          Keep temporary directory used to make the image.
  396.   -o outfile  Write to outfile.
  397.   -r root     Override ROOT setting in mkinitrd.conf.
  398.  
  399. See mkinitramfs(8) for further details.
  400. EOF
  401.     exit 1
  402.  
  403. }
  404.  
  405. # minimal supported kernel version
  406. check_minkver()
  407. {
  408.     local curversion initdir DPKG_ARCH minversion cm_x tmp
  409.  
  410.     curversion="${1}"
  411.     initdir="${2}"
  412.     if [ -z "${initdir}" ]; then
  413.         DPKG_ARCH=$(dpkg --print-installation-architecture)
  414.         case ${DPKG_ARCH} in
  415.             ia64|hppa)
  416.                 minversion="2.6.15"
  417.             ;;
  418.             *)
  419.                 minversion="2.6.12"
  420.             ;;
  421.         esac
  422.         if dpkg --compare-versions "${curversion}" lt "${minversion}"; then
  423.             echo "W: kernel ${curversion} too old for initramfs on ${DPKG_ARCH}" >&2
  424.             echo "W: not generating requested initramfs for kernel ${curversion}" >&2
  425.             exit 2
  426.         fi
  427.         return 0
  428.     fi
  429.     set_initlist
  430.     for cm_x in ${initlist}; do
  431.         # sed: keep last line starting with MINKVER=,
  432.         #      remove MINKVER= and trailing space
  433.         minver=$(sed '/^MINKVER=/!d;$!d;s/^MINKVER=//;s/[[:space:]]*$//' "${initdir}/${cm_x}")
  434.         if [ -z "${tmp}" ]; then
  435.             continue
  436.         elif dpkg --compare-versions "${curversion}" lt "${minver}"; then
  437.             echo "W: ${cm_x} hook script requires at least kernel version ${minver}" >&2
  438.             echo "W: not generating requested initramfs for kernel ${curversion}" >&2
  439.             exit 2
  440.         fi
  441.     done
  442. }
  443.